﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace AMS
{
    public class FormManager
    {
        internal Dictionary<Guid, Form> formCollection = new Dictionary<Guid, Form>();
        private Dictionary<Guid, Form> singletonCollection = new Dictionary<Guid, Form>();
        private Form mdiParent;

        public Form MDIParent
        {
            get { return mdiParent; }
            set { mdiParent = value; }
        }

        public void ShowForm(FormTypes formType, Int32 requestNumber)
        { 
            BaseForm baseForm;
            FormTypes currentType;

            switch (formType)
            {
                case FormTypes.LoadRequest:
                    baseForm = new frmLoadRequest();
                    currentType = FormTypes.LoadRequest;
                    break;
                case FormTypes.AboutBox:
                    baseForm = new frmAboutBox();
                    currentType = FormTypes.AboutBox;
                    break;
                case FormTypes.AnalysisMethod:
                    baseForm = new frmAnalysisMethods();
                    currentType = FormTypes.AnalysisMethod;
                    break;
                case FormTypes.Request:
                    baseForm = new frmRequest(requestNumber);
                    currentType = FormTypes.Request;
                    break;
                case FormTypes.SearchRequest:
                    baseForm = new frmSearchRequest();
                    currentType = FormTypes.SearchRequest;
                    break;
                case FormTypes.Requestors:
                    baseForm = new frmRequestors();
                    currentType = FormTypes.Requestors;
                    break;
                default:
                    throw new Exception("Ongeldig schermtype");
            }

            if (baseForm.IsSingleton)
            {
                // Make sure there is only one form alive at a time
                if (singletonCollection.ContainsKey(baseForm.Guid))
                {
                    BaseForm existingForm = (BaseForm)singletonCollection[baseForm.Guid];

                    //existingForm.Show();
                    existingForm.BringToFront();
                    baseForm = null; // Make sure to destroy the new instance
                }
                else
                {
                    singletonCollection.Add(baseForm.Guid, baseForm);
                    ShowForm(baseForm);
                }
            }
            else
            { 
                // Add the form to the collections and show it
                formCollection.Add(baseForm.Guid, baseForm);
                ShowForm(baseForm);
            }
        }

        public void DestroyForm(BaseForm baseForm)
        {
            formCollection.Remove(baseForm.Guid);
            singletonCollection.Remove(baseForm.Guid);
            

            baseForm.Close();
            baseForm.Dispose();
        }

        public void RefreshData(BaseForm sourceForm)
        { 
            // Call the refresh data method on all the open forms
            foreach (KeyValuePair<Guid,Form> item in formCollection)
            {
                if (sourceForm.Guid != item.Key)
                {
                    IUpdatableForm form = (IUpdatableForm)item.Value;
                    form.RefreshData();  
                }
            }

            foreach (KeyValuePair<Guid, Form> item in singletonCollection)
            {
                if (sourceForm.Guid != item.Key)
                {
                    IUpdatableForm form = (IUpdatableForm)item.Value;
                    form.RefreshData();
                }
            }
        }

        public void ShowError(Exception ex)
        {
            try
            {
                frmError error = new frmError();
                error.LoadException(ex);
                error.MdiParent = mdiParent;
                error.Show();
                error.BringToFront();

                Dictionary<Guid, Form>.KeyCollection keys = singletonCollection.Keys;
                foreach (Guid guid in keys)
                {
                    Form form = (Form)singletonCollection[guid];
                    form.SendToBack();
                }
            }
            catch (Exception showex)
            {
                MessageBox.Show("Er is een fatale fout opgetreden bij het tonen van een onderliggende foutmelding: " + showex.Message, "ShowError", MessageBoxButtons.OK);
            }
        }

        private void ShowForm(BaseForm baseForm)
        {
            baseForm.MdiParent = mdiParent;
            baseForm.Show();
            baseForm.LoadForm(this);
        }
    }

    public interface IUpdatableForm
    {
        void RefreshData();
    }

    public enum FormTypes
    { 
        LoadRequest,
        AboutBox,
        AnalysisMethod,
        Request,
        SearchRequest,
        Requestors
    }
}
